//
//  TTHomeHeaderCategoryView.m
//  TaoTuo
//
//  Created by LG on 2020/10/17.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTHomeHeaderCategoryView.h"
#import "TTUpDownButton.h"
#import "TTGoodsListViewController.h"

@interface TTHomeHeaderCategoryView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSMutableArray<UIView *> *dotViews;

@end

@implementation TTHomeHeaderCategoryView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.dotViews = [NSMutableArray array];
        
    }
    return self;
}

- (void)btnClick:(UIButton *)sender
{
    NSArray *array = [TTGoodsListViewController getAllCategoryTitles];
    NSInteger index = [array indexOfObject:sender.titleLabel.text];
    UIViewController *vc = [[CTMediator sharedInstance] goodsList_viewControllerWithTitle:sender.titleLabel.text index:index];
    [[LLRouteTool getCurrentNavigationController] pushViewController:vc animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / self.scrollView.width;
    UIView *dotView = self.dotViews[index];
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.center = dotView.center;
    }];
}

- (void)setDatasArray:(NSArray *)datasArray
{
    _datasArray = datasArray;
    
    [self removeAllSubviews];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    [self addSubview:_scrollView];
    
    
    CGFloat w = 50;
    CGFloat h = 84;
    CGFloat leftSpace = 12;
    CGFloat itemSpace = (self.width - leftSpace * 2 - w * 5) / 4;
    
    for (int i = 0; i < datasArray.count; i++) {
        NSString *title = datasArray[i];
        CGFloat x = leftSpace + (itemSpace + w) * i;
        TTUpDownButton *btn = [[TTUpDownButton alloc] initWithFrame:CGRectMake(x, 0, w, h)];
        [btn setImage:[UIImage imageNamed:title] forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:UIColorHex(#808080) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:btn];
    }
    
    NSInteger page = ceil(datasArray.count / 5.0);
    CGFloat dotH = 4;
    
    CGFloat dotBGViewW = (page + 1) * 10 + dotH * page;
    CGFloat dotBGViewX = (self.width - dotBGViewW) * 0.5;
    UIView *dotBgView = [[UIView alloc] initWithFrame:CGRectMake(dotBGViewX, h, dotBGViewW, 20)];
    [self addSubview:dotBgView];
    
    CGFloat y = (dotBgView.height - dotH) * 0.5;
    for (int i = 0; i < page; i++) {
        CGFloat x = 10 + (10 + dotH) * i;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, dotH, dotH)];
        view.backgroundColor = UIColorHex(#E6E6E6);
        view.layer.cornerRadius = 2;
        [dotBgView addSubview:view];
        [self.dotViews addObject:view];
    }
    
    _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, dotH)];
    _lineView.backgroundColor = UIColorHex(#8B57E6);
    _lineView.center = [self.dotViews.firstObject center];
    _lineView.layer.cornerRadius = 2;
    [dotBgView addSubview:_lineView];
    
    self.scrollView.contentSize = CGSizeMake(self.width * page, self.height);
    
}


@end
